// // 1.  定义一个Promise构造函数，传入`excutor`执行器函数
// function Promise(excutor){
//     // 2. 在构造函数内定义2个属性
//     //    1. `this.PromiseState` 保存promise状态
//     //    2. `this.PromiseResult`保存promise结果值
//     this.PromiseState = "pending";
//     this.PromiseResult = undefined;

//     // 3. 定义resolve、reject函数
//     //    1. 在resolve函数内修改`this.PromiseState` 为`fulfilled`，`this.PromiseResult`为value
//     //    2. 在reject函数内修改`this.PromiseState`为 `rejected`,`this.PromiseResult` 为 reason
//     let resolve = (value)=>{
//         this.PromiseState = 'fulfilled';
//         this.PromiseResult = value;
//     }
//     let reject = (reason)=>{
//         this.PromiseState = 'rejected';
//         this.PromiseResult = reason;
//     }

//      // 4. 调用`excutor`函数并传入resolve、reject函数
//      // 5. try捕获执行`excutor`函数时抛出错误
//         //    1. catch 内调用reject函数并传入err参数
//     try{
//      excutor(resolve,reject);
//     }catch(err){
//         reject(err)
//     }
// }

// // 1. 新建promise原型下then方法
// Promise.prototype.then = function(onResolve,onReject){

//     // 2. 判断对象状态为 "fulfilled"，调用onResolve函数，并传入结果值
//     if(this.PromiseState === "fulfilled"){
//         onResolve(this.PromiseResult);
//     }
//     // 3. 判断对象状态为 "rejected"，调用onReject函数，并传入结果值
//     if(this.PromiseState === 'rejected'){
//         onReject(this.PromiseResult)
//     }
// }

function Promise(excutor){
    this.PromiseState = 'pending'; 
    this.PromiseResult = undefined;

    // 定义
    let resolveFn = (value)=>{
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
    }

    let rejectFn = (reason)=>{
        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
    }

    try {
        excutor(resolveFn,rejectFn)
    } catch (error) {
        rejectFn(error)
    }
}

// 原型下方法
Promise.prototype.then = function(onResolve,onReject){
    // console.log(123);

    if(this.PromiseState === 'fulfilled'){
        onResolve(this.PromiseResult)
    }
    if(this.PromiseState === 'rejected'){
        onReject(this.PromiseResult)
    }

}




